#include "worker_x.h"
#include "thermalcam.h"
namespace RT{

static ThermalCamera* ThermalPtr = NULL;

Work_x::Work_x(WorkerParams wp):Worker(wp)                    
{
    if(ThermalPtr == NULL)
    {
        ThermalCameraArgs thermalArgs;
        thermalArgs.workerPtr = this;
        thermalArgs.iniPath = "../config/deviceConfig_thermal.ini";
        ThermalPtr = new ThermalCamera(thermalArgs);
        ThermalPtr->mHckRun_tcp();
    }
}


Work_x::~Work_x()
{
    if(ThermalPtr)
    {
        delete  ThermalPtr;
        ThermalPtr = NULL;
    }
    std::cout << "~Work_x()>>>>>>>>>>>>>>>>>>>" << std::endl;
}







//需要根据具体任务更改，一般作为服务端需要配置处理函数
void Work_x::m_process(void* work_ptr)
{
    Work_x* wPtr = (Work_x*)work_ptr;

    //获取信息
    WorkerInfo wio;
    if(wPtr->m_popTaskQueue(wio) == -1 ){
        m_logerPtr->m_WARNING("m_popTaskQueue(wio) == -1");
        return;
    }
    sem_wait(&wPtr->m_semQueueNumForTask);

    const int connfd = wio.connfd;   //可能为-1
    Json::Value root = wio.root;
    const std::string &imgBuf_string = wio.imgDataBuf_str;


    //开始处理
    if(     root["DetectCommand"]["Command"] == "UpdatePic" 
            && root["DetectCommand"]["Args"].isMember("Positions")  
            && root["DetectCommand"]["Args"].isMember("File_Path")
            && root["DetectCommand"]["Args"].isMember("Time") 
            && root["DetectCommand"]["Args"].isMember("Task_ID")  
            && root["DetectCommand"]["Args"].isMember("ImageProcessMode")
        )
    { 
        const int robotPosition       = atoi( root["DetectCommand"]["Args"]["Positions"].asString().c_str() );
        const string image_path       = root["DetectCommand"]["Args"]["File_Path"].asString();  //GCB21040085_20210812103758.jpg
        const string time             = root["DetectCommand"]["Args"]["Time"].asString();
        const string task_id          = root["DetectCommand"]["Args"]["Task_ID"].asString();
        const string imageProcessMode = root["DetectCommand"]["Args"]["ImageProcessMode"].asString();


    }

}


void Work_x::m_processCallBack(Json::Value root){
    std::cout << "recv workInfo: " << JsonTool::mJson2string(root) << std::endl;
    int connfd = this->mConnfd;

    if( root.isMember("HeartbeatACK") ){
        if(mDebug){
            m_logerPtr->m_DEBUG(JsonTool::mJson2string(root), "", true, false);
        }
        return;
    }

    if( root.isMember("WorkInfo") ){
        //解析json中的目标id，从而获取该targetId的connfd，并将该次接收的信息发送到connfd
        const string targetId = root["WorkInfo"]["targetId"].asString();
        const string selfId = root["WorkInfo"]["selfId"].asString();
        const string callbackId = root["WorkInfo"]["callbackId"].asString();
        const Json::Value data = root["WorkInfo"]["data"];
        
        if(targetId == "null" || selfId == "null"){
            if(this->mDebug){
                m_logerPtr->m_DEBUG(JsonTool::mJson2string(root["WorkInfo"]), "");
            }
            return; 
        }
        
        //处理函数
        //detectCommand->Command
        if(data.isMember("DetectCommand") )
        {
            // WorkerInfo wi;
            // // wi.workingClass = this;
            // if(callbackId == "null"){
            //     // 不发送
            //     wi.connfd = -1;
            // }
            // else{
            //     wi.connfd = connfd;
            // }
            
            // wi.root = data;
            // if(root.isMember("image")){
            //     wi.imgDataBuf_str = root["image"].asString();
            // }
            // wi.context = this;
            // this->m_pushTaskQueue(wi);
            // sem_post(&this->m_semQueueNumForTask);
            // m_addTaskToThreadPool(mPool, Work_x::m_process, this, "other-process", false);
        }
        //<<< 
        return;
    }
    if( root.isMember("Error") ){
        std::cout << "error of the workInfo : " << JsonTool::mJson2string(root) << std::endl;
        return;
    } 
}    



// void Work_x::m_matToString(cv::Mat srcImg, string &str, const string imgType)
// {
//     //cv::resize(srcImg, srcImg, cv::Size(round(srcImg.cols * rate), round(srcImg.rows * rate)));
//     std::vector<unsigned char> buff;
//     cv::imencode(imgType.c_str(), srcImg, buff);
//     str.resize(buff.size());
//     memcpy(&str[0], buff.data(), buff.size());
// }
// int Work_x::m_stringToMat(cv::Mat &dstImg, const string &orgImgStr, const int imgChannels)
// {   
//     const int channels = imgChannels;
//     if(channels == 3){
//         cv::Mat tempMat(1, orgImgStr.size(), CV_8UC3, (char*)orgImgStr.data());
//         dstImg = cv::imdecode(tempMat, CV_LOAD_IMAGE_COLOR);
//     }
//     else if(channels == 1){
//         cv::Mat tempMat(1, orgImgStr.size(), CV_8UC1, (char*)orgImgStr.data());
//         dstImg = cv::imdecode(tempMat, CV_LOAD_IMAGE_COLOR);
//     }
//     else{
//         m_logerPtr->m_ERROR("string to Mat error\n");
//         return -1;
//     }
//     return 0;
// }





}//namespace

